package com.zhang.network.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.dto.BaseResponse;
import com.zhang.network.constant.BizResponse;
import com.zhang.network.model.Friend;
import com.zhang.network.model.NetworkUser;
import com.zhang.network.model.Recommend;
import com.zhang.network.model.User;
import com.zhang.network.model.recommend.RecommendDTO;
import com.zhang.network.model.recommend.RecommendVO;
import com.zhang.network.service.FriendService;
import com.zhang.network.service.RecommendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Resource
    RecommendService recommendService;

    @Resource
    FriendService friendService;

    @GetMapping("/pageRecommend")
    public BaseResponse<Page<RecommendVO>> pageRecommend(RecommendDTO recommendDTO) {
        log.info("推荐消息");
        Page<RecommendVO> recommendPage = recommendService.pageRecommend(recommendDTO);
        if (recommendPage.getRecords().size() > 0) {
            return BizResponse.success(recommendPage);
        }
        return BizResponse.noMessage("无推荐信息");
    }

    @GetMapping("/pageFriendRequest")
    public BaseResponse<Page<RecommendVO>> pageFriendRequest(RecommendDTO recommendDTO) {
        Page<RecommendVO> friendRequestPage = recommendService.pageFriendRequest(recommendDTO);
        if (friendRequestPage.getRecords().size() > 0) {
            return BizResponse.success(friendRequestPage);
        }
        return BizResponse.noMessage("无人脉添加请求");
    }

    @GetMapping("/agreeFriendReq")
    public BaseResponse agreeFriendReq(RecommendVO recommendVO) {
        log.info("请求参数：{}", recommendVO.toString());
        List<Friend> list = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", recommendVO.getId())
                .eq("friend_Id", recommendVO.getToId()));
        if (list.size() > 0) {
            return BizResponse.warn("已拥有此人脉");
        }
        Friend friend1 = new Friend();
        friend1.setFriendId(recommendVO.getToId());
        friend1.setUserId(recommendVO.getId());
        friend1.setRank(recommendVO.getRecommendRank() + recommendVO.getToRank());
        boolean result1 = friendService.save(friend1);
        Friend friend2 = new Friend();
        friend2.setFriendId(recommendVO.getId());
        friend2.setUserId(recommendVO.getToId());
        friend2.setRank(recommendVO.getRecommendRank() + recommendVO.getToRank());
        boolean result2 = friendService.save(friend2);
        boolean result3 = recommendService.removeById(recommendVO.getPrimaryKey());
        if (result1 && result2 && result3) {
            return BizResponse.success();
        }
        return BizResponse.fail();

    }


    @GetMapping("/sendRecommend")
    public BaseResponse send(Recommend recommend) {
        List<Recommend> list = recommendService.list(new QueryWrapper<Recommend>()
                .eq("user_id", recommend.getUserId())
                .eq("recommend_Id", recommend.getRecommendId())
                .eq("friend_Id", recommend.getFriendId()));
        if (list.size() > 0) {
            return BizResponse.warn("已发送过请求");
        }
        boolean result = recommendService.save(recommend);
        if (result) {
            return BizResponse.success();
        }
        return BizResponse.fail("发送请求失败");

    }

    @GetMapping("/sendFriendRequest")
    public BaseResponse sendFriendRequest(Recommend recommend) {
        List<Friend> list1 = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", recommend.getRecommendId())
                .eq("friend_Id", recommend.getFriendId()));
        if (list1.size() > 0) {
            return BizResponse.warn("已拥有此人脉");
        }
        List<Recommend> list = recommendService.list(new QueryWrapper<Recommend>()
                .eq("user_id", recommend.getUserId())
                .eq("recommend_Id", recommend.getRecommendId())
                .eq("friend_Id", recommend.getFriendId()));
        if (list.size() > 0) {
            return BizResponse.warn("已发送过请求");
        }
        boolean result = recommendService.save(recommend);
        if (result) {
            return BizResponse.success();
        }
        return BizResponse.fail("发送请求失败");

    }

    @GetMapping("/acceptRecommend")
    public BaseResponse acceptRecommend(RecommendVO recommendVO) {
        List<Friend> list = friendService.list(new QueryWrapper<Friend>()
                .eq("user_id", recommendVO.getToId())
                .eq("friend_Id", recommendVO.getId()));
        if (list.size() > 0) {
            return BizResponse.warn("已拥有此人脉");
        }
        List<Recommend> list1 = recommendService.list(new QueryWrapper<Recommend>()
                .eq("user_id", recommendVO.getToId())
                .eq("recommend_Id", recommendVO.getToId())
                .eq("friend_Id", recommendVO.getId()));
        if (list1.size() > 0) {
            return BizResponse.warn("已发送过请求");
        }
        boolean result1 = recommendService.removeById(recommendVO.getPrimaryKey());
        Recommend recommend = new Recommend();
        recommend.setFriendId(recommendVO.getId());
        recommend.setRecommendId(recommendVO.getToId());
        recommend.setUserId(recommendVO.getToId());
        recommend.setReference(recommendVO.getReference());
        recommend.setRecommendRank(recommendVO.getRecommendRank());
        recommend.setToRank(recommendVO.getToRank());
        boolean result = recommendService.save(recommend);
        if (result && result1) {
            return BizResponse.success();
        }
        return BizResponse.fail("发送请求失败");

    }

    @GetMapping("/delete")
    public BaseResponse delete(RecommendVO recommendVO) {
        boolean result = recommendService.removeById(recommendVO.getPrimaryKey());
        if (result) {
            return BizResponse.success();
        }
        return BizResponse.fail();

    }



}

